package net.qiqbframework.domains.record.domain;

import net.qiqbframework.eventhandling.DomainEventMessage;
import net.qiqbframework.modelling.domain.AggregateRoot;
import net.qiqbframework.modelling.domain.EntityIdentifier;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@AggregateRoot
@Getter
@Setter
public class DomainEventRecord {

    @EntityIdentifier
    private String id;

    private String eventId;

    /**
     * 处理实例名称
     */
    protected String instanceName;
    /**
     * 处理应用名称
     */
    protected String applicationName;


    private DomainEventMessage<?> domainEventInfo;

    /**
     * 接收到的时间
     */
    protected LocalDateTime receiveTime;
    /**
     * 处理状态：
     * preprocess:准备处理
     * processing：处理中
     * process_success:处理成功
     * process_fail:处理失败
     */
    protected String status;
    /**
     * 处理信息
     */
    protected String processMsg;

    protected LocalDateTime createTime;
    /**
     * 开始处理时间
     */
    protected LocalDateTime beginProcessTime;

    protected LocalDateTime endProcessTime;

    protected String listenerId;


    public void beginProcess() {
        this.beginProcessTime = LocalDateTime.now();
        this.status = "processing";
    }

    public void endProcess(boolean processSuccess, String processMsg) {
        if (processSuccess) {
            this.status = "process_success";
        } else {
            this.status = "process_fail";
        }
        this.endProcessTime = LocalDateTime.now();
        this.processMsg = processMsg;
    }

    public boolean isSuccess() {
        return this.status.equals("process_success");
    }
}
